package br.com.pesca.dao;

import br.com.pesca.entity.Formulario;
import java.util.Date;
import java.util.List;
import javax.persistence.Query;

public class FormularioDAO extends BaseDAO<Formulario> {

    public Long consultaUltimoCodigo() {
        Query query = getEntityManager()
                .createQuery(
                        "SELECT Count(*) FROM "
                        + Formulario.class.getName()
                );
        return (Long) query.getSingleResult();
    }

    @SuppressWarnings("unchecked")
    public List<Formulario> consultaCodigo(String codigo) {
        Query query = getEntityManager()
                .createQuery(
                        "SELECT formulario FROM "
                        + Formulario.class.getName()
                        + " AS formulario WHERE formulario.codigo like :codigo ");
        query.setParameter("codigo", "%" + codigo + "%");
        return query.getResultList();
    }

    @SuppressWarnings("unchecked")
    public List<Formulario> consultaData(Date data) {
        Query query;
        //if (data2 == null) {
        query = getEntityManager()
                .createQuery(
                        "SELECT formulario FROM "
                        + Formulario.class.getName()
                        + " AS formulario WHERE data_formulario = :data");
        query.setParameter("data", new java.sql.Date(data.getTime()));
        return query.getResultList();
    }

    @SuppressWarnings("unchecked")
    public List<Formulario> consultaData(int mes, int ano) {
        Query query;
        query = getEntityManager()
                .createQuery(
                        "SELECT formulario FROM "
                        + Formulario.class.getName()
                        + " AS formulario WHERE month(data_formulario) = :mes"
                        + " and year(data_formulario) = :ano");
        query.setParameter("mes", mes);
        query.setParameter("ano", ano);
        return query.getResultList();
    }

}
